/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supporter;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author HoangNam
 */
public class Supporter {

    public static String md5Encrypt(String str) {
        String result = "";
        try{
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(str.getBytes());
            BigInteger dis = new BigInteger(1, md5.digest());
            result = dis.toString(32);
        } catch(Exception ex){
            JOptionPane.showMessageDialog(null, "Error in process encypt!", "Error", JOptionPane.ERROR_MESSAGE, null);
        }
        return result;
    }
    public String getCurrentDate() {
        Date now = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        return (dateFormat.format(now.getTime()));
    }

    public String getRandomNumber() {
        Calendar now = Calendar.getInstance();
        String second = String.valueOf(now.get(Calendar.SECOND));
        String minute = String.valueOf(now.get(Calendar.MINUTE));
        String hour = String.valueOf(now.get(Calendar.HOUR_OF_DAY));
        String date = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
        String month = String.valueOf(now.get(Calendar.MONTH) + 1);
        String year = String.valueOf(now.get(Calendar.YEAR));

        return (second + minute + date + month + hour + year);
    }

//    public static int getPeriodOfTwoDays(String dateOfBorrowing, String dateOfReturning){
//
//        System.out.println(dateOfBorrowing + "----------" + dateOfReturning);
//        String[] arr_dateOfBorrowing = dateOfBorrowing.split("/");
//        String[] arr_dateOfReturning = dateOfReturning.split("/");
//        Calendar c1 = Calendar.getInstance();
//        Calendar c2 = Calendar.getInstance();
//
//        c1.set(Integer.valueOf(arr_dateOfBorrowing[2]), Integer.valueOf(arr_dateOfBorrowing[1]), Integer.valueOf(arr_dateOfBorrowing[0]));
//        c2.set(Integer.valueOf(arr_dateOfReturning[2]), Integer.valueOf(arr_dateOfReturning[1]) , Integer.valueOf(arr_dateOfReturning[0]));
//
//        return (int) ((c2.getTime().getTime() - c1.getTime().getTime()) / (24 * 3600 * 1000));
//    }
//    public static void getdueDate(){
//        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
//        Calendar c1 = Calendar.getInstance();
//        Date d1 = new Date();
//        System.out.println("Todays date in Calendar Format : "+c1);
//        System.out.println("c1.getTime() : "+c1.getTime());
//        System.out.println("c1.get(Calendar.YEAR): " + c1.get(Calendar.YEAR));
//        System.out.println("Todays date in Date Format : "+d1);
//        c1.set(2008,02 ,20);
//        System.out.println("c1.set(2008,02 ,20) : "+c1.getTime());
//        c1.add(Calendar.DATE,20);
//        System.out.println("Date + 20 days is : "+ sdf.format(c1.getTime()));
//        System.out.println();
//    }
    public boolean compareTwoDays(String dateOfBorrowing, String effectiveDate) {
        String[] arr_dateOfBorrowing = dateOfBorrowing.split("/");
        String[] arr_effectiveDate = effectiveDate.split("/");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.set(Integer.valueOf(arr_dateOfBorrowing[2]), Integer.valueOf(arr_dateOfBorrowing[1]), Integer.valueOf(arr_dateOfBorrowing[0]));
        c2.set(Integer.valueOf(arr_effectiveDate[2]), Integer.valueOf(arr_effectiveDate[1]), Integer.valueOf(arr_effectiveDate[0]));
        if (c2.before(c1) | c1.equals(c1)) {
            return true;
        } else {
            return false;
        }
    }
}
